package com.spring.ext.demos.ext;

import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.ConstructorArgumentValues;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.stereotype.Component;

import java.util.ArrayList;

@Component
public class MyBeanFactoryPostPorcess implements BeanFactoryPostProcessor {

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        GenericBeanDefinition userBeanDefinition =(GenericBeanDefinition) beanFactory.getBeanDefinition("User");
        //设置作用域
        userBeanDefinition.setScope(BeanDefinition.SCOPE_SINGLETON);
        //设置属性值
        ConstructorArgumentValues values = new ConstructorArgumentValues();
        values.addIndexedArgumentValue(0,"test");
        values.addIndexedArgumentValue(1,"777");
        userBeanDefinition.setConstructorArgumentValues(values);
        //设置对象
        MutablePropertyValues propertyValues = new MutablePropertyValues();
        propertyValues.addPropertyValue("partner",new ArrayList<>());
        userBeanDefinition.setPropertyValues(propertyValues);

    }
}
